package com.example.carbon.controller;

import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.dto.ProduceModelDtoInsert;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.MaterialInfoServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 物料档案
 */
@RestController
@RequestMapping("/api/material")
@Slf4j
public class MaterialInfoController {
    @Resource
    MaterialInfoServer materialInfoServer;

    /**
     * 根据类别来获取物料/零件+成品
     * @param classification
     * @return
     */
    @RequestMapping("/get/info")
    public RestBean selectAllProductData(String classification){
        try {
            if (classification == null){
                return RestBean.success(materialInfoServer.selectAllInfo(""));
            }
            return RestBean.success(materialInfoServer.selectAllInfo(classification));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("获取数据失败");
        }
    }

    @RequestMapping("/del/{id}")
    public RestBean delById(@PathVariable("id") int id){
        try {
            return RestBean.success(materialInfoServer.delInfoById(id));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("删除失败，服务器问题");
        }
    }

    @PostMapping("insert")
    public RestBean insertNewInfo(@RequestBody ProduceModelDtoInsert produceModelDtoInsert  ){
        try {
            return RestBean.success(materialInfoServer.InsertNewInfo(produceModelDtoInsert));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("插入失败，请联系管理员");
        }
    }

}
